package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsMchntBlackListMapper;
import cc.rengu.igas.cops.common.entity.CopsMchntBlackListInfo;
import cc.rengu.igas.cops.common.entity.CopsMchntRiskInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户黑名单表(T_B_MCHNT_BLACKLIST_INFO) 操作实现
 *
 * @author keyi
 * @date 2020/3/27.
 */
public class CopsMchntBlackListMapperImpl implements CopsMchntBlackListMapper {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public List<CopsMchntBlackListInfo> selectOpmsMchntBlackListInfoByInstIdMchntNo(String instId, String mchntNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsGetMchntBlackListByInstIdMchntNo");

        CopsMchntBlackListInfo opmsMchntBlackListInfo = new CopsMchntBlackListInfo();
        opmsMchntBlackListInfo.setInstId(instId);
        opmsMchntBlackListInfo.setMchntNo(mchntNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, opmsMchntBlackListInfo, CopsMchntBlackListInfo.class);
        return objects.stream().map(item -> (CopsMchntBlackListInfo) item).collect(Collectors.toList());
    }

    @Override
    public boolean insert2BlackListInfo(CopsMchntRiskInfo mchntRiskInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsInsert2BlackListInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntRiskInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记黑名单信息失败,mchntName:<{}>", mchntRiskInfo.getMchntName());
            return false;
        }
        return true;

    }

    public List<CopsMchntBlackListInfo> checktMchntBlackListInfoByCerNo(String artifCertType, String artifCertNo, String licenceType, String licenceCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsChecktMchntBlackListInfoByCerNo");
        CopsMchntBlackListInfo copsMchntBlackListInfo = new CopsMchntBlackListInfo();
        copsMchntBlackListInfo.setArtifCertType(artifCertType);
        copsMchntBlackListInfo.setArtifCertNo(artifCertNo);
        copsMchntBlackListInfo.setLicenceType(licenceType);
        copsMchntBlackListInfo.setLicenceCode(licenceCode);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, copsMchntBlackListInfo, CopsMchntBlackListInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsChecktMchntBlackListInfoByCerNo");
            objectList = dbsUtil.dbsFetchLimitListData(sqlStr, copsMchntBlackListInfo, CopsMchntBlackListInfo.class);
            if (null == objectList || objectList.isEmpty()) {
                rglog.warn("库中不存在该数据，需要插入");
                return null;
            }
        }
        return objectList.stream().map(item -> (CopsMchntBlackListInfo) item).collect(Collectors.toList());
    }

    public int updateMchntBlackListInfo(CopsMchntRiskInfo mchntRiskInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsUpdateMchntBlackListInfo");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntRiskInfo, null);
        if (0 != iReturnCode) {
            rglog.error("修改黑名单商户信息失败,mchntName:<{}>,iReturnCode:<{}>", mchntRiskInfo.getMchntName(), iReturnCode);
        }
        return iReturnCode;
    }


}
